package com.aduerr.v1shouyin.unioncard;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.pedant.SweetAlert.SweetAlertDialog;
import com.aduerr.v1shouyin.util.Utils;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/7/22 10:11
 * @change
 * @chang time
 * @class describe
 */

public abstract class BaseUnionFragment extends Fragment implements IView {

    protected CompositeDisposable mCompositeDisposable;
    protected Context mContext;
    SweetAlertDialog progressDialog;
    Unbinder unbinder;
    private View mLayoutView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(mLayoutView != null) {
            ViewGroup parent = (ViewGroup) mLayoutView.getParent();
            if(parent != null) {
                parent.removeView(mLayoutView);
            }
        } else {
            mLayoutView = getCreateView(inflater, container);
            unbinder = ButterKnife.bind(this, mLayoutView);
            mCompositeDisposable = new CompositeDisposable();
            initProgressDialog();
            initBase();
            initView();
        }

        return mLayoutView;
    }

    private View getCreateView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(getLayoutId(), container, false);
    }


    protected void initBase() {

    }

    protected void initProgressDialog() {

        progressDialog = new SweetAlertDialog(mContext, SweetAlertDialog.PROGRESS_TYPE);
        progressDialog.getProgressHelper().setBarColor(Color.parseColor("#005fee"));
        progressDialog.setTitleText("让数据为你所用");
        progressDialog.setCancelable(false);
    }

    protected abstract int getLayoutId();

    protected void initView() {
    }


    private void showLoadingDialogBySafety(String message, boolean cancelable) {
        progressDialog.setTitleText(message);
        progressDialog.setCancelable(cancelable);
        if(!progressDialog.isShowing()) {
            progressDialog.show();
        }
    }

    public void showLoadingDialog(final String message, final boolean cancelable) {
        if(Utils.isMainThread()) {
            showLoadingDialogBySafety(message, cancelable);
        }
    }

    public void showLoadingDialog(String message) {
        showLoadingDialog(message, false);
    }


    public void dismissLoadingDialog() {
        if(progressDialog.isShowing()) {
            if(Utils.isMainThread()) {
                progressDialog.dismiss();
            }
        }
    }

    public final void showToast(final CharSequence text, final int duration) {
        if(Utils.isMainThread()) {
            Toast.makeText(mContext, text, duration).show();
        }
    }

    public final void showToast(@StringRes int resID, int duration) {
        showToast(getString(resID), duration);
    }

    public final void showToast(@StringRes int resID) {
        showToast(resID, Toast.LENGTH_SHORT);
    }

    public final void showToast(CharSequence text) {
        showToast(text, Toast.LENGTH_SHORT);
    }

    public void addDisposable(Disposable d) {
        mCompositeDisposable.add(d);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(unbinder != null)
            try {
                unbinder.unbind();
            } catch(Exception e) {
                e.printStackTrace();
            }
        mCompositeDisposable.clear();
    }


    @Override
    public void removeDisposable(Disposable d) {
        mCompositeDisposable.remove(d);
    }
}
